/**
 * @param e 图片对象
 */
function showBigImage(e) {
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        shadeClose: true, //点击阴影关闭
        area: [$(e).width + 'px', $(e).height + 'px'], //宽高
        content: "<img src=" + $(e).attr('src') + " />"
    });
}

function downloadIamge() {
    var url = $("#spreadCode").attr("src");
    window.open(url)
}

/**
 * 编辑直播课程详情对话框
 */
var CourseInfoInfoDlg = {
    data: {
        courseId: "",
        courseUrl: "",
        courseName: "",
        courseNums: "",
        status: "",
        noticeType: "",
        noticeTime: "",
        createUser: "",
        createTime: "",
        updateUser: "",
        updateTime: ""
    }
};

layui.use(['layer', 'form', 'admin', 'ax','laydate'], function () {
    var $ = layui.jquery;
    var $ax = layui.ax;
    var form = layui.form;
    var admin = layui.admin;
    var layer = layui.layer;
    var laydate = layui.laydate;

    form.verify({
        namelength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255))
                    sum=sum+1;
                else
                    sum=sum+2;
            }
            if (sum > 80) {
                return '课程名称最多只能是80个字符或者40个中文字';
            }
        },
        urllength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255))
                    sum=sum+1;
                else
                    sum=sum+2;
            }
            if (sum > 100) {
                return '课程链接最多只能是100个字符';
            }
        },
        addresslength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255))
                    sum=sum+1;
                else
                    sum=sum+2;
            }
            if (sum > 50) {
                return '课程地址最多只能50个字符或者25个中文字';
            }
        },
        remarklength: function (value) {
            var i,sum;
            sum=0;
            for(i=0;i<value.length;i++){
                if ((value.charCodeAt(i)>=0) && (value.charCodeAt(i)<=255))
                    sum=sum+1;
                else
                    sum=sum+2;
            }
            if (sum > 500) {
                return '备注最多只能500个字符或者250个中文字';
            }
        },
        noticeTimeReq: function (value) {
            if($("#noticeType option:selected").val()=="SYS_NOTICE"){
                if($("#noticeTime").val()==""){
                    return '通知时间必填';
                }
            }
        }

    });

    //获取详情信息，填充表单
    var ajax = new $ax(Feng.ctxPath + "/courseInfo/detail?courseInfoId=" + Feng.getUrlParam("courseInfoId"));
    var result = ajax.start();
    form.val('courseInfoForm', result.data);
    $("#spreadCode").attr("src","https://api.qrserver.com/v1/create-qr-code?data="+result.data.spreadUrl)
    // $("#downloadSpreadCode").attr("href","https://api.qrserver.com/v1/create-qr-code?data="+result.data.spreadUrl)
    // $("#downloadSpreadCode").attr("download",result.data.courseName+".jpg")


    //初始化时间
    laydate.render({
        elem: '#courseTime', //指定元素
        type:'datetime',
        format:'yyyy-MM-dd HH:mm:ss',
        min: 'new Date()'
    });
    laydate.render({
        elem: '#noticeTime', //指定元素
        type:'datetime',
        format:'yyyy-MM-dd HH:mm:ss',
        min:   $("#courseTime").val()==""? 'new Date()':$("#courseTime").val()

    });
    laydate.render({
        elem: '#createTime' //指定元素
        ,type:'datetime'
        ,format:'yyyy-MM-dd HH:mm:ss'
        ,value: new Date()

    });
    laydate.render({
        elem: '#updateTime' //指定元素
        ,type:'datetime'
        ,format:'yyyy-MM-dd HH:mm:ss'
        ,value: new Date()
    });


    //select 监听
    form.on('select(noticeType)', function(data){
        if(data.value == 'MAN_NOTICE'){
            $("#noticeTimeModel").hide();
        }else{
            $("#noticeTimeModel").show();
        }
    });


    // 让当前iframe弹层高度适应
    admin.iframeAuto();

    // 点击courseInfoName时 弹出树，不需要请删掉
    $('#courseInfoName').click(function () {
        var formName = encodeURIComponent("parent.CourseInfoInfoDlg.data.courseName");
        var formId = encodeURIComponent("parent.CourseInfoInfoDlg.data.courseId");
        var treeUrl = encodeURIComponent("/courseInfo/courseInfoTreeList");

        layer.open({
            type: 2,
            title: '父级直播课程选择',
            area: ['300px', '200px'],
            content: Feng.ctxPath + '/system/commonTree?formName=' + formName + "&formId=" + formId + "&treeUrl=" + treeUrl,
            end: function () {
                $("#courseInfoId").val(CourseInfoInfoDlg.data.courseId);
                $("#courseInfoName").val(CourseInfoInfoDlg.data.courseName);
            }
        });
    });




    if( $("#noticeType option:selected").val()=="MAN_NOTICE"){
        $("#noticeTimeModel").hide();
    }else{
        $("#noticeTimeModel").show();
    }

    // 表单提交事件
    form.on('submit(btnSubmit)', function (data) {
        var ajax = new $ax(Feng.ctxPath + "/courseInfo/edit", function (data) {
            Feng.success("修改成功！");
            //返回
            window.location.href = Feng.ctxPath + "/courseInfo?courseInfoId=" + $("#courseId").val();
            /** 窗口返回前页模式
             //传给上个页面，刷新table用
             //admin.putTempData('formOk', true);

             //关掉对话框
             admin.closeThisDialog();
             */
        }, function (data) {
            Feng.error("修改失败！" + data.responseJSON.message)
        });
        ajax.set(data.field);
        ajax.start();
        return false;//返回
    });

    //返回按钮，需要同步修改页面按钮id
    $("#backupPage").click(function () {
        window.location.href = Feng.ctxPath + "/courseInfo?courseInfoId=" + $("#courseId").val();
    });
});